package com.fs.cv.service.impl;

import com.fs.cv.dao.RecordDao;
import com.fs.cv.pojo.pojodao.Record;
import com.fs.cv.pojo.pojodao.Role;
import com.fs.cv.pojo.pojodao.User;
import com.fs.cv.service.TeachingService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @作者: zh
 * @时间: 2020/9/29 15:55
 * @描述: 对应实现类
 */
@Service
public class TeachingServiceImpl implements TeachingService {
    @Autowired
    RecordDao recordDao;
    @Override
    public Record[][] getRecords(Date date,String aClass,int op,String[] dates,Map<String,String> who) {
        int n=7,m=3;
        Record[][] records=new Record[n][m];
        //计算date所在日期的周第一天是多少，最后一天是多少
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        int add=cal.get(Calendar.DAY_OF_WEEK)-1;
        if(add==0)add=7;
        cal.add(Calendar.DATE,-add+1);
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        Date time=cal.getTime();
        String begin=df.format(time);
        cal.add(Calendar.DATE,7);
        String end=df.format(cal.getTime());
        //获取当前用户的类型后向数据库查询出集合
        Subject subject=SecurityUtils.getSubject();
        User user=(User)subject.getSession().getAttribute("user");
        System.out.println("!"+user);
        List<String> roles= (List<String>)subject.getSession().getAttribute("roles");
        //要返回的数据
        List<Record> recordList=new ArrayList<>();
        //根据op查询
        if(op==0&&roles.contains("教务主任")){
            //是教务主任，根据aClass查询
             recordList=recordDao.selectByDate(begin,end,"class_name",aClass);
             who.put("who",aClass);
        }else if (op==1&&roles.contains("班主任")){
            //是班主任,先查是哪个班的班主任，再根据班级查询
            String class_name=recordDao.selectClassByMaster(user.getUserId()).get("class_name");
            aClass=class_name;
            who.put("who",aClass);
            recordList=recordDao.selectByDate(begin,end,"class_name",class_name);
        }else{
            //是授课教师或者普通学生,先查教师表,以此判断类型
            if(roles.contains("任课教师")){
                String teacher=recordDao.selectTeacherNameByUserId(user.getUserId()).get("teacher_name");
                recordList=recordDao.selectByDate(begin,end,"teacher_name",teacher);
                who.put("who",teacher);
            }
            else if(roles.contains("学生")){
                //学生表查询写在此处
                //who=student;
            }

        }
        //将结果转化成数组
        for(Record r:recordList){
            cal.setTime(r.getDate());
            records[cal.get(Calendar.DAY_OF_WEEK)-2==-1?6:cal.get(Calendar.DAY_OF_WEEK)-2][r.getIndex()]=r;
        }
        //将这周的七天日期格式化后分别存进dates里
        cal.setTime(time);
        for(int i=0;i<7;i++){
            dates[i]=df.format(cal.getTime());
            cal.add(Calendar.DATE,1);
        }
        return records;
    }
    @Override
    public Record getRecord(Integer id) {
        return recordDao.selectById(id);
    }
    @Override
    public boolean modifyRecord(Record record) {
        Map<String,Integer> map=new HashMap<String,Integer>(){
            {
                put("teacher_id",recordDao.selectTeacherByName(record.getTeacher()).get("teacher_id"));
                put("course_id",recordDao.selectCourseByName(record.getCourse()).get("course_id"));
                put("class_id",recordDao.selectClassByName(record.get_class()).get("class_id"));
                put("classroom_id",recordDao.selectClassroomByName(record.getClassroom()).get("classroom_id"));
                put("record_id",record.getId());
            }
        };
        return recordDao.updateRecord(map)>0;
    }

    @Override
    public boolean removeRecord(Integer id,Map<String,Object> map) {
        Record record1 = recordDao.selectById(id);
        Calendar cal=Calendar.getInstance();
        cal.setTime(record1.getDate());
        int add=cal.get(Calendar.DAY_OF_WEEK)-1;
        if(add==0)add=7;
        map.put("i",add-1);
        map.put("j",record1.getIndex());
        return recordDao.deleteRecord(id)>0;
    }

    @Override
    public boolean addRecord(Record record) {
        Map<String,Object> map=new HashMap<String,Object>(){
            {
                put("teacher_id",recordDao.selectTeacherByName(record.getTeacher()).get("teacher_id"));
                put("course_id",recordDao.selectCourseByName(record.getCourse()).get("course_id"));
                put("class_id",recordDao.selectClassByName(record.get_class()).get("class_id"));
                put("classroom_id",recordDao.selectClassroomByName(record.getClassroom()).get("classroom_id"));
                put("record_date",record.getDate());
                put("record_index",record.getIndex());
            }
        };
        boolean f=recordDao.insertRecord(map)>0;
        Number number=(Number)map.get("id");
        record.setId(number.intValue());
        return f;
    }

    @Override
    public boolean adjustRecord(Record record,Map<String,Object> map) {
        Record record1 = recordDao.selectById(record.getId());
        Calendar cal=Calendar.getInstance();
        cal.setTime(record1.getDate());
        int add=cal.get(Calendar.DAY_OF_WEEK)-1;
        if(add==0)add=7;
        map.put("i",add-1);
        map.put("j",record1.getIndex());
        record.setCourse(record1.getCourse());
        record.setTeacher(record1.getTeacher());
        record.set_class(record1.get_class());
        record.setClassroom(record1.getClassroom());
        Map<String,Object> xx=new HashMap<String,Object>(){
            {
                put("record_date",record.getDate());
                put("record_index",record.getIndex());
                put("record_id",record.getId());
            }
        };
        return recordDao.adjustRecord(xx)>0;
    }

    @Override
    public List<Map<String, Object>> searchBlurry(String keyI, String keyWord) {
        List<Map<String,Object>> map=null;
        switch (keyI){
            case "_class":
                User user=(User)SecurityUtils.getSubject().getSession().getAttribute("user");
                Integer departmentId = recordDao.selectDepartmentByRegistrarId(user.getUserId()).get("department_id");
                System.out.println(departmentId);
                map=recordDao.selectClassBlurry(keyWord,departmentId);
                break;
            case "teacher":
                map=recordDao.selectTeacherBlurry(keyWord);
                break;
            case "course":
                map=recordDao.selectCourseBlurry(keyWord);
                break;
            case "classroom":
                map=recordDao.selectClassroomBlurry(keyWord);
                break;
        }
        return map;
    }
}
